package com.iretailer.report.util;

import java.util.Collection;
import java.util.List;
import java.util.function.Predicate;

/**
 * @Program: report
 * @Description:
 * @Author: Bennie
 * @Date: 2021-02-07 17:14
 **/


public class CommonUtil {

    public static <T> T find(Iterable<T> ts, T t) {
        for (T next : ts) {
            if (next.equals(t)) {
                return next;
            }
        }
        return null;
    }

    public static <T> T find(Iterable<T> ts, Predicate<T> p) {
        for (T next : ts) {
            if (p.test(next)) {
                return next;
            }
        }
        return null;
    }

    public static <T> int findIndex(List<T> ts, T t) {
        for (int i = 0; i < ts.size(); i++) {
            if (ts.get(i).equals(t)) {
                return i;
            }
        }
        return -1;
    }

    public static <T> int findIndex(T[] ts, T t) {
        for (int i = 0; i < ts.length; i++) {
            if (ts[i].equals(t)) {
                return i;
            }
        }
        return -1;
    }

    public static <T> int findIndex(List<T> ts, Predicate<T> p) {
        for (int i = 0; i < ts.size(); i++) {
            if (p.test(ts.get(i))) {
                return i;
            }
        }
        return -1;
    }

    public static <T> int findIndex(T[] ts, Predicate<T> p) {
        for (int i = 0; i < ts.length; i++) {
            if (p.test(ts[i])) {
                return i;
            }
        }
        return -1;
    }

    public static <T> boolean isNotEmpty(Collection<T> ts) {
        return ts != null && ts.size() > 0;
    }
}
